/**
 * 功能描述 卡片
 */

define(function (require, exports, module) {
	var Form = require("form");
	var store =require("store");
	var activeUser = store.get("user");
	var aw = require("ajaxwrapper");
	
    var instance = {
        initComponent: function (params,widget) {
			var appForm=new Form ({
				parentNode:".J-appForm",
				saveaction:function(){
					var form = widget.get("form");
					var appGrid = widget.get("appGrid");
					var appForm = widget.get("appForm");
					var data =appForm.getData() ;
//					if(data.sequenceNumber != 0 && data.approvalRuleRegister.pkApprovalRuleRegister == 1){
//						Dialog.alert({
//							content:"提交方只能在第0位！"
//						});
//        				return;
//					}
					data.approvalUser = null
					data.approvalDefine =form.getValue("pkApprovalDefine");
					aw.saveOrUpdate("api/approvaldefineitem/saveall",aw.customParam(data)+"&pkPersonalInfo="+appForm.getValue("approvalUser"),function(data){
							form.setValue("setupTime",moment().format("YYYY-MM-DD"));
							form.setValue("user.name",activeUser.name);
							widget.show(".J-appGrid").hide(".J-appForm");
							appGrid.refresh({
								"approvalDefine.pkApprovalDefine":widget.get("pkApprovalDefine"),
	            				fetchProperties:"approvalDefineItem.*," +
	            						"personalInfo.*," +
	            						"approvalDefineItem.approvalRole.*," +
	            						"approvalDefineItem.approvalUser.*" ,
							});
							widget.get("grid").refresh();
					});
					
				},
				cancelaction:function(){
					widget.show(".J-appGrid").hide(".J-appForm");
				},
				model:{
					id:"approvalDefineItemForm",
					items:[
					{
						name:"pkApprovalDefineItem",
						type:"hidden"
					},{
						name:"version",
						defaultValue:"0",
						type:"hidden"
					},{
						name:"sequenceNumber",
						label:"序号",
						validate:["required","zeronumber"]
					},{
						name:"approvalRole",
						label:"审批角色",
						type:"select",
						key:"pkRole",
						value:"name",
						url: "api/role/query",
						validate:["required"],
						events : {
							"change" : function(){
								var role = appForm.getValue("approvalRole");
								aw.ajax({
									url:"api/attendant/queryattendant",
									data:{
										"commonUser.operator.roles.pkRole":role,
										"servicePoint.pkServicePoint":activeUser.servicePoint.pkServicePoint,
										fetchProperties:"*,personalInfo.*"
										},
									dataType:"json",
									success:function(data){
										var temp = [];
										if(data.length>0){
											for(var i in data){
												var dat = {};
												dat.pkPersonalInfo = data[i].personalInfo.pkPersonalInfo;
												dat.name = data[i].personalInfo.name;
												temp.push(dat);
											}
										}
										appForm.setData("approvalUser",temp);
										widget.get("grid").refresh();
									}
								});
							}
						}
					},{
						name:"approvalUser",
						label:"审批人",
						type:"select",
						key:"pkPersonalInfo",
						value:"name",
					}]
				}
			});
       	 	widget.set("appForm",appForm);
        }
    };
    module.exports = instance;
});
